package com.lst.servlet;

import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.druid.util.StringUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lst.base.entity.BaseRequest;
import com.lst.base.entity.BaseResponse;
import com.lst.common.CommCode;
import com.lst.entity.request.CompanionLogReqPara;
import com.lst.model.Companion;
import com.lst.model.CompanionLog;
import com.lst.model.Message;
import com.lst.service.CompanionLogService;
import com.lst.service.CompanionService;
import com.lst.service.MessageService;
import com.lst.utils.RequestUtils;


/**
 * @ClassName: CompanionLogAddServlet
 * @Description: 嘿哈报名接口
 * 
 */
public class CompanionLogAddServlet extends BaseServlet {

	/**
	 * @Fields serialVersionUID
	 */
	private static final long serialVersionUID = 1L;

	private CompanionLogService companionLogService = ctx.getBean(CompanionLogService.class);
	
	private CompanionService companionService = ctx.getBean(CompanionService.class);
	
	private MessageService messageService = ctx.getBean(MessageService.class);

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		doPost(request, response);
	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		Date startDate = new Date();
		BaseResponse resPara = new BaseResponse();

		try {
			CompanionLogReqPara reqPara = getReqPara(request);
		
		if(isCheckField(reqPara,resPara)){
			       Integer userid = Integer.valueOf(reqPara.getUserid());
			       Integer sourceId = Integer.valueOf(reqPara.getSourceId());
			
			        Map<String,Object> map=new HashMap<String,Object>();
			        map.put("sourceId", sourceId);
			        map.put("createUser", userid);
			        CompanionLog companionLog=companionLogService.queryByMap(map);
			        if(companionLog!=null){
			        	resPara.setCode(CommCode.M_A000000);
	    				resPara.setMessage("已经添加过感兴趣");
			        }else{
			        	CompanionLog md = new CompanionLog();
	    				md.setCreateUser(userid);
	    				md.setCreateDate(startDate);
	    				md.setSourceId(sourceId);
	    				companionLogService.insert(md);
	  
	    				resPara.setCode(CommCode.M_Y000000);
	    				resPara.setMessage(CommCode.M_Y000001);
	    				
	    				Companion companion=companionService.selectByPrimaryKey2(sourceId);
	    				if(companion!=null){
	    					Message message=new Message();
	    					message.setSendId(userid);
	    					message.setContent("嘿，我对你发的嘿哈很感兴趣！");
	    					message.setReceptionId(companion.getCompanionCreateUser());
	    					message.setIsDelete(false);
	    					message.setReadStatus(false);
	    					message.setCreateDate(startDate);
	    			        messageService.insert(message);
	    				}
	    				
			        }
		  }
		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0001);

			logger.error("doPost error: ", e);
		}
		
		try {
			response.setContentType("application/json;charset=utf-8");
			response.setHeader("pragma", "no-cache");
			response.setHeader("cache-control", "no-cache");

			Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").excludeFieldsWithoutExposeAnnotation().serializeNulls().create();
			String jsonResult = gson.toJson(resPara);
		//	logger.info("CompanionLogAddServlet jsonResult:" + jsonResult);

			PrintWriter out = response.getWriter();
			out.print(jsonResult);
			out.flush();
			out.close();

		//	logger.info("doPost end run(s): " + DateUtil.calLastedTime(startDate));
		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0003);

			logger.error("doPost error: ", e);
		}
	}

	@Override
	public CompanionLogReqPara getReqPara(HttpServletRequest request) {

		CompanionLogReqPara reqPara = null;

		try {
			BaseRequest baseRequest = RequestUtils.getRequestPara(request, new CompanionLogReqPara());

			reqPara = (CompanionLogReqPara) baseRequest.clone();

			String userid = request.getParameter("userid");
			String sourceId = request.getParameter("sourceId");

//			logger.info("reqPara userid:" + userid);
//			logger.info("reqPara sourceId: " + sourceId);
			
	        reqPara.setUserid(userid);
	        reqPara.setSourceId(sourceId);
		} catch (Exception e) {
			logger.error("getReqPara error: ", e);
		}

		return reqPara;
	}

	@Override
	public boolean isCheckField(BaseRequest reqPara, BaseResponse resPara) {
		boolean flag = false;

		CompanionLogReqPara cdReqPara = (CompanionLogReqPara) reqPara;
		BaseResponse cdResPara = (BaseResponse) resPara;

		if (StringUtils.isEmpty(cdReqPara.getSourceId())) {
			cdResPara.setCode(CommCode.M_A000000);
			cdResPara.setMessage("嘿哈活动id为空");
		}else if (StringUtils.isEmpty(cdReqPara.getUserid())) {
			cdResPara.setCode(CommCode.M_A000000);
			cdResPara.setMessage(CommCode.M_P000002);
		} else {
			flag = true;
		}

		return  flag;
	}
	

}
